<template>
  <el-container>
    <!-- 左侧   -->
    <el-aside width="200px">
      <img src="../assets/loginBox.svg">
      <p class="imgTitle">欢迎使用动力云客系统</p>
    </el-aside>
    <!-- 右侧   -->
    <el-main>
      <div class="loginTitle">
        欢迎登录
      </div>
      <el-form ref="loginRefForm" :model="user" label-width="120px" :rules="loginRules">
        <el-form-item label="账号" prop="loginAct">
          <el-input clearable v-model="user.loginAct"/>
        </el-form-item>

        <el-form-item label="密码" prop="loginPwd">
          <el-input type="password" show-password v-model="user.loginPwd"/>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login()">登录</el-button>
        </el-form-item>


        <el-form-item>
          <el-checkbox label="记住我" v-model="user.rememberMe"/>
        </el-form-item>

      </el-form>
    </el-main>
  </el-container>
</template>

<script>
import {doGet, doPost} from "../http/httpRequest.js";
import {getTokenName, messageTip, removeToken} from "../util/util.js";

export default {
  //组件名称
  name: "LoginView",
  //定义页面使用到的变量
  data() {
    return {
      //对变量定义：{}
      user: {},
      //字符串变量定义:''
      name: '',
      //数字变量定义: 0
      age: 0,
      //数组变量定义:[]
      arr: [],
      //list集合对象定义：[{}]
      userList: [{}],
      //定义验证登录表单规则
      loginRules: {
        loginAct: [
          {required: true, message: '请输入登录账号', trigger: 'blur'}
        ],
        loginPwd: [
          {required: true, message: '请输入登录密码', trigger: 'blur'},
          {min: 6, max: 16, message: '登录密码长度6-16位', trigger: 'blur'},
        ]
      }
    }
  },

  mounted() {
    this.freeLogin();
  },

  methods: {
    //登录函数
    login() {
      //提交前保证输入框的合法性
      this.$refs.loginRefForm.validate((isValid) => {
        //isValid验证后的结果 true 通过
        if (isValid) {
          //验证通过提交
          let formData = new FormData();
          formData.append("loginAct", this.user.loginAct);
          formData.append("loginPwd", this.user.loginPwd);
          formData.append("rememberMe", this.user.rememberMe);
          doPost("/api/login", formData).then((rep) => {
            if (rep.data.code === 200) {
              //登陆成功 提示一下
              messageTip("登陆成功", "success");
              //清楚历史token数据
              removeToken();
              if (this.user.rememberMe === true){
                window.localStorage.setItem(getTokenName(),rep.data.data);
              }else {
                window.sessionStorage.setItem(getTokenName(),rep.data.data);
              }
              //跳转到系统主页面
              window.location.href = "/dashboard/";
            } else {
              //登录失败 提示一下
              messageTip("登陆失败", "error");
            }

          });
        }
      })
    },
    freeLogin(){
      let token = window.localStorage.getItem(getTokenName());
      //token不是空
      if (token){
        doGet("/api/login/free",{}).then(response => {
          if (response.data.code === 200){
            window.location.href = "/dashboard/";
          }
        })
      }
    }

  }
}
</script>

<style scoped>
.el-aside {
  background: #1a1a1a;
  width: 40%;
  text-align: center;
}

.el-main {
  height: calc(100vh);
}

img {
  height: 500px;
}

.imgTitle {
  color: #f9f9f9;
  font-size: 30px;
}

.el-form {
  width: 60%;
  margin: auto;
}

.loginTitle {
  text-align: center;
  margin-top: 300px;
  margin-bottom: 20px;
  font-weight: bold;
}

.el-button {
  width: 100%;
}
</style>